// index.js
const app = getApp()

const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({
  data: {
    statusTabs: ['全部', '服务中', '跟进中', '服务结束', '今日联系'],
    currentStatusTab: 0,
    listData: [],
    showFilterPopup: false,
    timeTags: ['今日', '昨日', '本周', '上周', '本月', '上月', '自定义时间'],
    sourceTags: ['上门客户', '客户转介', '广告'],
    currentTimeTag: -1,
    currentSourceTag: -1
  },

  onLoad: function() {
    this.initMockData();
    this.fetchListData(0);
  },

  initMockData() {
    this.mockDatabase = {
      all: [
        {
          avatar: '/images/default-avatar.png',
          name: '张三',
          follower: '陈小姐',
          status: '服务中',
          address: '广东省深圳市南山区科技园',
          contract: '已签订合同：2024-001'
        },
        {
          avatar: '/images/default-avatar.png',
          name: '李四',
          follower: '王先生',
          status: '跟进中',
          address: '广东省广州市天河区珠江新城',
          contract: '暂未签订合同'
        }
      ],
      服务中: [
        {
          avatar: '/images/default-avatar.png',
          name: '张三',
          follower: '陈小姐',
          status: '服务中',
          address: '广东省深圳市南山区科技园',
          contract: '已签订合同：2024-001'
        }
      ],
      跟进中: [
        {
          avatar: '/images/default-avatar.png',
          name: '李四',
          follower: '王先生',
          status: '跟进中',
          address: '广东省广州市天河区珠江新城',
          contract: '暂未签订合同'
        }
      ],
      服务结束: [
        {
          avatar: '/images/default-avatar.png',
          name: '王五',
          follower: '赵经理',
          status: '服务结束',
          address: '广东省东莞市松山湖',
          contract: '已结束合同：2023-156'
        }
      ],
      今日联系: [
        {
          avatar: '/images/default-avatar.png',
          name: '赵六',
          follower: '林小姐',
          status: '跟进中',
          address: '广东省惠州市惠城区',
          contract: '暂未签订合同'
        }
      ]
    };
  },

  fetchListData(tabIndex) {
    const tabName = this.data.statusTabs[tabIndex];
    let data = [];
    
    if (tabIndex === 0) {
      data = this.mockDatabase.all;
    } else {
      data = this.mockDatabase[tabName] || [];
    }

    this.setData({
      listData: data,
      currentStatusTab: tabIndex
    });
  },

  switchTab(e) {
    const index = Number(e.currentTarget.dataset.index);
    this.fetchListData(index);
  },

  showScreening() {
    wx.showToast({
      title: '筛选功能开发中',
      icon: 'none'
    })
  },

  showSort() {
    wx.showToast({
      title: '排序功能开发中',
      icon: 'none'
    })
  },

  // 显示筛选弹框
  showFilter() {
    this.setData({
      showFilterPopup: true
    });
  },

  // 选择时间标签
  selectTimeTag(e) {
    const index = e.currentTarget.dataset.index;
    this.setData({
      currentTimeTag: index
    });
  },

  // 选择来源标签
  selectSourceTag(e) {
    const index = e.currentTarget.dataset.index;
    this.setData({
      currentSourceTag: index
    });
  },

  // 重置筛选
  resetFilter() {
    this.setData({
      currentTimeTag: -1,
      currentSourceTag: -1,
      showFilterPopup: false
    });
  },

  // 确认筛选
  confirmFilter() {
    // 这里处理筛选逻辑
    this.setData({
      showFilterPopup: false
    });
  },

  // 切换状态tab
  switchStatusTab(e) {
    const index = e.currentTarget.dataset.index;
    this.setData({
      currentStatusTab: index
    });
  },

  onShow() {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0  // 首页对应的 index
      })
    }
  }
})